package com.wuzuqing.android.eightdiagrams.model

import com.wuzuqing.android.eightdiagrams.term.Lunar
import com.wuzuqing.android.eightdiagrams.term.Solar
import com.wuzuqing.android.eightdiagrams.utils.DataUtils
import java.util.Arrays

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 26.7.24 9:54 AM
 * @Description: 请写描述
 */
class QiMenDunJiaModel {
    var lunar: Lunar? = null
    var solar: Solar? = null
    var list: MutableList<QiMenDunJiaItem> = ArrayList()
    var isYangDun = false
    var shiShu = 0
    var juShu = 0
    var dingJu: String = ""
    var xunShou: String = ""
    var zhiGan: String = ""
    var isShiGanJia = false

    //值班的地支宫位
    var zhiMen = ""
    var zhiXin = ""
    //空亡
    var kongWang = ""
    //驿马
    var yiMa = ""
    var zhiGongNum = 0
    var originGongNum = 0
    var menGongShu = 0

    init {
        list.add(QiMenDunJiaItem("巽", 4, "辰巳", "").setMenXing("杜门", "天辅"))
        list.add(QiMenDunJiaItem("离", 9, "午", "").setMenXing("景门", "天英"))
        list.add(QiMenDunJiaItem("坤", 2, "未申", "\n").setMenXing("死门", "天芮"))
        list.add(QiMenDunJiaItem("震", 3, "卯", "").setMenXing("伤门", "天冲"))
        list.add(QiMenDunJiaItem("中", 5, "", "").setMenXing("", "天禽"))
        list.add(QiMenDunJiaItem("兑", 7, "酉", "\n").setMenXing("惊门", "天柱"))
        list.add(QiMenDunJiaItem("艮", 8, "丑寅", "").setMenXing("生门", "天任"))
        list.add(QiMenDunJiaItem("坎", 1, "子", "").setMenXing("休门", "天蓬"))
        list.add(QiMenDunJiaItem("乾", 6, "戌亥", "").setMenXing("开门", "天心"))
    }

    fun handleItem() {
        lunar?.baZi?.also { baZhi ->
            val shiGan = baZhi[3].getOrNull(0).toString()
            val shiZhi = baZhi[3].getOrNull(1).toString()
            val riGan = baZhi[2].getOrNull(0).toString()

            this.list.firstOrNull { it.tianPanGan .contains(riGan)  }?.apply {
                isRiGan = true
            }
            this.list.firstOrNull { it.tianPanGan.contains(shiGan) }?.apply {
                isShiGan = true
            }
            this.list.firstOrNull { it.shen == "值符" }?.apply {
                isZhiFu = true
            }
            this.list.firstOrNull { it.men == zhiMen }?.apply {
                isZhiShi = true
            }
            kongWang = DataUtils.getLiuJiaKongWang(baZhi[3])
            yiMa = DataUtils.getYiMa(shiZhi)
            this.list.firstOrNull { it.diPanZhi.contains(yiMa) }?.apply {
                isYiMa = true
            }
            this.list.firstOrNull {
                it.diPanZhi.contains(kongWang[0]) || it.diPanZhi.contains(kongWang[1])
            }?.apply {
                isKongWang = true
            }

        }
        this.list.forEach {
            val sb = StringBuilder()
            it.isJiXing = DataUtils.isJiXing(it.tianPanGan, it.gongNum) || DataUtils.isJiXing(it.diPanGan, it.gongNum)
            it.isPoGong = DataUtils.isPoGong(it.men, it.gongNum)
            it.isRuMu = DataUtils.isRuMu(it.tianPanGan, it.gongNum) || DataUtils.isRuMu(it.diPanGan, it.gongNum)
            if (it.isKongWang){
                sb.append("空")
            }
            if (it.isYiMa){
                sb.append("马")
            }
            if (it.isShiGan){
                sb.append("时")
            }
            if (it.isRiGan){
                sb.append("日")
            }
            if (it.isRuMu || it.isPoGong || it.isJiXing) {
                sb.append("\n")
            }
            if (it.isRuMu) {
                sb.append("墓")
            }
            if (it.isPoGong) {
                sb.append("迫")
            }
            if (it.isJiXing) {
                sb.append("邢")
            }
            it.flagStr = sb.toString()
            it.juInfo = DataUtils.getQiMenDunJiaJu(this, it)
        }
    }

    companion object {
        var shenArr = Arrays.asList("值符", "腾蛇", "太阴", "六合", "白虎", "玄武", "九地", "九天")

        @JvmField
        var menArr = Arrays.asList("休门", "生门", "伤门", "杜门", "景门", "死门", "惊门", "开门")

        @JvmField
        var xingArr = Arrays.asList("天蓬", "天任", "天冲", "天辅", "天英", "天芮", "天柱", "天心")
    }
}